home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / WINDOWS / CLIPSTAC.ARJ / DYNARRAY.H < prev    next >
C/C++ Source or Header  |  1992-03-16  |  1KB  |  67 lines

  1. // dynarray.h RHS 1/7/92
  2.  
  3. #if !defined(DYNARRAY_H)
  4. #define DYNARRAY_H
  5.  
  6. #include<stdio.h>
  7. #include<memory.h>
  8.  
  9. template<class TYPE>
  10. class DynArray
  11.     {
  12.     TYPE   *TArray;
  13.     int size;
  14.     int num;
  15.  
  16. public:
  17.     void Init(int n)
  18.         {
  19.         TArray = new TYPE[size = n];    
  20.         num = 0;
  21.         }
  22.     DynArray(int n)
  23.         {
  24.         Init(n);
  25.         }
  26.     DynArray(void)
  27.         {
  28.         TArray = NULL;
  29.         num = 0;
  30.         size = 0;
  31.         }
  32.     ~DynArray()                 {   delete[] TArray;    }
  33.     TYPE& operator[](int i)     {   return TArray[i];   }
  34.     NumItems(void)              {   return size;        }
  35.     void ReSize(int newsize);
  36.     void Delete(TYPE t);
  37.     void Add(TYPE t);
  38.     };
  39.  
  40. template<class TYPE>
  41. void DynArray<TYPE>::Add(TYPE t)
  42.     {
  43.     if(num == size)
  44.         ReSize(size+10);
  45.     TArray[num++] = t;
  46.     }
  47.  
  48. template<class TYPE>
  49. void DynArray<TYPE>::ReSize(int newsize)
  50.     {
  51.     if(newsize == size)
  52.         return;
  53.  
  54.     TYPE *temp = new TYPE[newsize];
  55.     if(!temp)
  56.         return;
  57.     memcpy(temp,TArray,
  58.         ( (newsize > size) ? (size*sizeof(TYPE)) : (newsize*sizeof(TYPE)) ));
  59.     delete TArray;
  60.     TArray = temp;
  61.     size = newsize;
  62.     }
  63.         
  64. #endif
  65.  
  66.  
  67.